﻿using Newtonsoft.Json;
using RentHouse.IService;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using RentHouse.Service;
using RentHouse.DTO;
using RentHouse.Admin.App_Start;

namespace RentHouse.Admin.Controllers
{
    public class AdminController : Controller
    {
        IAdminUsersService adminUserService;
        public AdminController(IAdminUsersService adminUserService)
        {
            this.adminUserService = adminUserService;

        }

        //返回全部信息
        [LoggingActionFilter]
        public string GetAll(int page, int limit)
        {
            //Response.Headers.Add("Access-Control-Allow-Origin", "*");


            //字典类（用于存储数据）
            Dictionary<string, object> map = new Dictionary<string, object>();

            map.Add("code", "0");//数据格式或类型
            map.Add("msg", "用户列表信息");//数据声明
            map.Add("count", adminUserService.GetList().Count);//数据总数（用于分页）

            map.Add("data", adminUserService.GetList()
                .OrderBy(u => u.Id)//分页前必须根据某一列进行排序，保证数据顺序唯一
                .Skip((page - 1) * limit)//执行当前页起始数据
                .Take(limit));//每页条数

            return JsonConvert.SerializeObject(map);
            
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="adminUserDTO"></param>
        /// <returns></returns>
        [LoggingActionFilter]
        public string Create(AdminUserDTO adminUserDTO) {

            //Response.Headers.Add("Access-Control-Allow-Origin", "*");

            return adminUserService.Create(adminUserDTO); 
            
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="adminUserDTO"></param>
        /// <returns></returns>
        public string Delete(AdminUserDTO adminUserDTO) {

            //Response.Headers.Add("Access-Control-Allow-Origin", "*");

            return adminUserService.Delete(adminUserDTO);
        }

        /// <summary>
        /// 用户修改
        /// </summary>
        /// <param name="adminUserDTO"></param>
        /// <returns></returns>
        public string Update(AdminUserDTO adminUserDTO)
        {

            //Response.Headers.Add("Access-Control-Allow-Origin", "*");

            return adminUserService.Update(adminUserDTO);
        }


        /// <summary>
        /// 用户登录
        /// </summary>
        /// <param name="adminUserDTO"></param>
        /// <returns></returns>
        [LoggingActionFilter]
        public string Login(AdminUserDTO adminUserDTO)
        {

            //Response.Headers.Add("Access-Control-Allow-Origin", "*");

            return adminUserService.Login(adminUserDTO); 
        }











    }
}